package com.example.spinnerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener {

	private TextView view;
	private Spinner spinner1;
	private Spinner spinner2;
	private static final String[] m = { "A型", "B型", "O型", "AB型", "其他" };
	private String[] c;
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner2 = (Spinner) findViewById(R.id.spinner2);
		view = (TextView) findViewById(R.id.textView1);
		// 将可选内容与ArrayAdapter连接起来
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, m);

		// 设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// 将adapter 添加到spinner中
		spinner1.setAdapter(adapter);

		// 添加事件Spinner事件监听
		spinner1.setOnItemSelectedListener(this);
		spinner2.setOnItemSelectedListener(this);

		// 获取arrays中的数据
		c = getResources().getStringArray(R.array.country_arrays);
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub

		String str = null;
		int key = arg0.getId();
		switch (key) {
		case R.id.spinner1:

			str = "你的血型是：" + m[arg2];
			break;
		case R.id.spinner2:

			str = "你的国家是：" + c[arg2];
			break;

		default:
			break;
		}

		Log.d("wenhao", "into the cont " + str);
		view.setText(str);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
